package com.bangwz.start;

import java.util.Date;

import org.tio.client.AioClient;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Aio;
import org.tio.core.Node;

import com.bangwz.handler.MyClientAIOHander;
import com.bangwz.pack.Const;
import com.bangwz.pack.MyPacket;

public class ClientStart {

	public static Node server_node = new Node(Const.SERVER, Const.PORT);

	public static ClientAioHandler clientAioHandler = new MyClientAIOHander();

	public static ClientAioListener clientAioListener = null;

	// 5m auto conn
	public static ReconnConf reconnConf = new ReconnConf(5000L);

	public static ClientGroupContext clientGroupContext = new ClientGroupContext(clientAioHandler, clientAioListener);

	public static AioClient aioClient = null;

	public static ClientChannelContext clientChannelContext = null;

	public static void main(String[] args) throws Exception {
		clientGroupContext.setHeartbeatTimeout(Const.TIME_OUT);
		aioClient = new AioClient(clientGroupContext);
		clientChannelContext = aioClient.connect(server_node);
		send();
	}

	private static void send() throws Exception {
		for (int i = 0; i < 1000; i++) {
			MyPacket myPacket = new MyPacket();
			Date date = new Date();
			myPacket.setBody(new String("Hello Dongyaz" + date).getBytes(MyPacket.CHARSET));
			Aio.send(clientChannelContext, myPacket);
			Thread.sleep(500);
		}
	}
}
